package com.lenovo.leos.filebrowser.netdisk;

import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;

import com.lenovo.leos.filebrowser.R;
import com.lenovo.leos.filebrowser.netdisk.LoadedData;
import com.lenovo.leos.filebrowser.netdisk.NetFile;

public class NetDiskOpResult {
	private static final String TAG = "NetDiskOpResult";

	private static String msContent = null;

	public NetDiskOpResult(String content) {
		if (null == content || 0 == content.trim().length()) {
			Log.e(TAG, "invalid content in NetDiskOpResult");
			return;
		} else
			msContent = content;
	}

	public ArrayList<NetFile> GetOnJSonItemListAll() throws JSONException {
		String jsonstream = msContent;
		if (null == jsonstream || jsonstream.trim().length() == 0) {
			return null;
		} else {
			ArrayList<NetFile> curItemData = new ArrayList<NetFile>();
			JSONObject tjson = new JSONObject(jsonstream);
			if (tjson.getLong("result") == 1) {
				JSONArray tjsonarray = tjson.getJSONArray("filelist");
				for (int i = 0; i < tjsonarray.length(); i++) {
					JSONObject ttjson = tjsonarray.getJSONObject(i);
					NetFile item = new NetFile(ttjson.getString("fileid"),
							ttjson.getString("name"),
							tjson.getString("parent"), NetFile.Type.file);
					curItemData.add(item);
					Log.e(TAG, "the file name is " + item.getName());
				}

				JSONArray tdirjsonarray = tjson.getJSONArray("dirlist");
				Log.e(TAG, "dir list is " + tdirjsonarray.toString());
				for (int i = 0; i < tdirjsonarray.length(); i++) {
					JSONObject ttjson = tdirjsonarray.getJSONObject(i);
					NetFile item = new NetFile(ttjson.getString("dirid"),
							ttjson.getString("dirname"),
							tjson.getString("parent"), NetFile.Type.dir);
					curItemData.add(item);
					Log.e(TAG, "the dir name is " + item.getName());
				}

				return curItemData;
			} else {
				return null;
			}
		}
	}
	
	/**
	 * 计算大小
	 * @param lSize
	 * @return
	 */
	private String transferSize(long lSize) {
		String size;
		int iPoint;
		if (lSize < 1024) {
			size = lSize + "B";
		} else if (lSize < 1024 * 1024) {
			size = String.valueOf(lSize / 1024.0);
			iPoint = size.indexOf(".");
			size = size.substring(0, iPoint + 2) + "K";
		} else if (lSize < 1024 * 1024 * 1024) {
			size = String.valueOf(lSize / 1024.0 / 1024);
			iPoint = size.indexOf(".");
			size = size.substring(0, iPoint + 2) + "M";
		} else if (lSize < 1024L * 1024L * 1024L * 1024L) {
			size = String.valueOf(lSize / 1024.0 / 1024 / 1024);
			iPoint = size.indexOf(".");
			size = size.substring(0, iPoint + 2) + "G";
		} else if (lSize < 1024L * 1024L * 1024L * 1024L * 1024L) {
			size = String.valueOf(lSize / 1024.0 / 1024 / 1024 / 1024);
			iPoint = size.indexOf(".");
			size = size.substring(0, iPoint + 2) + "T";
		} else {
			size = "未知";
		}

		return size;
	}

	public synchronized String getTotalspace() throws JSONException {
		String totalspace;
		String jsonstream = msContent;
		if (null == jsonstream || jsonstream.trim().length() == 0) {
			return null;
		} else {
			JSONObject tjson = new JSONObject(jsonstream);
			if (tjson.getLong("result") == 1) {
				totalspace = new String(tjson.getString("totalspace"));
				long data = Long.parseLong(totalspace);
				totalspace = transferSize(data);
				Log.d(TAG, "totalspace--------------------" + totalspace);
				return totalspace;
			} else {
				return null;
			}
		}
		//
	}

	public synchronized String getFreespace() throws JSONException {
		String useSpace;
		String jsonstream = msContent;
		if (null == jsonstream || jsonstream.trim().length() == 0) {
			return null;
		} else {
			JSONObject tjson = new JSONObject(jsonstream);
			if (tjson.getLong("result") == 1) {

				useSpace = new String(tjson.getString("space"));
				String totalspace = new String(tjson.getString("totalspace"));
				long total = Long.parseLong(totalspace);
				long use = Long.parseLong(useSpace);
				long free = total - use;
				String freeSpace = transferSize(free);
				SumTask.getInstance().setNetFreeSpace(
						Long.valueOf(free));
				Log.d(TAG, "freeSpace--------------------" + freeSpace);
				return freeSpace;
			} else {
				return null;
			}
		}
		//
	}

	NetFile GetNewDir() throws JSONException {
		String jsonstream = msContent;
		if (null == jsonstream || jsonstream.trim().length() == 0) {
			return null;
		} else {
			JSONObject tjson = new JSONObject(jsonstream);
			if (tjson.getLong("result") == 1) {
				NetFile NewDir = new NetFile(tjson.getString("id"),
						tjson.getString("name"), tjson.getString("parent"),
						NetFile.Type.dir);
				return NewDir;

			} else {
				return null;
			}

		}

	}

	NetFile getRenameItemResult() throws JSONException {
		String jsonstream = msContent;
		if (null == jsonstream || jsonstream.trim().length() == 0) {
			return null;
		} else {
			JSONObject tjson = new JSONObject(jsonstream);
			if (tjson.getLong("result") == 1) {
				if ("file".equals(tjson.getString("type"))) {
					NetFile retItem = new NetFile(tjson.getString("fileid"),
							tjson.getString("filename"),
							tjson.getString("parent"), NetFile.Type.file);
					return retItem;
				} else if ("dir".equals(tjson.getString("type"))) {
					NetFile retItem = new NetFile(tjson.getString("dirid"),
							tjson.getString("dirname"),
							tjson.getString("parent"), NetFile.Type.dir);
					Log.i(TAG, "the new dir name is " + retItem.getName());
					return retItem;
				} else {
					return null;
				}
			} else {
				return null;
			}
		}
	}

	boolean GetDelDirStutes() throws JSONException {
		String jsonstream = msContent;
		if (null == jsonstream || jsonstream.trim().length() == 0) {
			return false;
		} else {
			JSONObject tjson = new JSONObject(jsonstream);
			if (tjson.getLong("result") == 1) {
				return true;
			} else {
				return false;
			}
		}
	}

	boolean GetResult() throws JSONException {
		String jsonstream = msContent;
		if (null == jsonstream || jsonstream.trim().length() == 0) {
			return false;
		} else {
			JSONObject tjson = new JSONObject(jsonstream);
			if (tjson.getLong("result") == 1) {
				return true;
			} else {
				return false;
			}
		}
	}

	public NetFile GetRootNetFile() throws JSONException {

		String jsonstream = msContent;
		if (null == jsonstream || jsonstream.trim().length() == 0) {
			return null;
		} else {
			JSONObject tjson = new JSONObject(jsonstream);
			if (tjson.getLong("result") == 1) {
				NetFile netfileitem = new NetFile(tjson.getString("dirid"),
						"/", "", NetFile.Type.dir);
				return netfileitem;
			} else {
				return null;
			}
		}
	}

	public ArrayList<LoadedData> GetLoadDataList() throws JSONException {

		String jsonstream = msContent;
		if (null == jsonstream || jsonstream.trim().length() == 0) {
			return null;
		} else {
			JSONObject tjson = new JSONObject(jsonstream);
			if (tjson.getLong("result") == 1) {

			} else {

			}
		}
		ArrayList<LoadedData> curLoadData = new ArrayList<LoadedData>();
		return curLoadData;
	}

	public static String strGetErrorMsg() {
		Log.e(TAG, "==> strGetErrorMsg");
		String jsonstream = msContent;
		String strError = "noerror";
		String strTimeOut = "网络异常，请重新登录！";//time out
		try {
			if (null == jsonstream || jsonstream.trim().length() == 0) {
				return null;
			} else {
				JSONObject tjson;

				tjson = new JSONObject(jsonstream);

				if (tjson.getLong("result") == 1) {
					return strError;
				}else if(tjson.getLong("result") == 2){
					return strTimeOut;
				}else {
					strError = tjson.getString("errmsg");
					return strError;
				}
			}
		} catch (JSONException e) {
			e.printStackTrace();
			return strError;
		}
	}

}